package fate;


import java.io.EOFException;
import java.io.IOException;
import java.io.InputStream;
import java.io.RandomAccessFile;

public class BinaryUtils {

    public static long readulong(RandomAccessFile in, byte[] buffer) throws IOException {
        int n = in.read(buffer, 0, 8);
        if (n != 8) {
            throw new EOFException();
        }
        // TODO: assert negative
        return (((long)(buffer[7] & 255) << 56) +
                ((long)(buffer[6] & 255) << 48) +
		        ((long)(buffer[5] & 255) << 40) +
                ((long)(buffer[4] & 255) << 32) +
                ((long)(buffer[3] & 255) << 24) +
                ((long)(buffer[2] & 255) << 16) +
                ((long)(buffer[1] & 255) <<  8) +
                ((long)(buffer[0] & 255) <<  0));
    }
    
    public static long readulong(InputStream in, byte[] buffer) throws IOException {
        int n = in.read(buffer, 0, 8);
        if (n != 8) {
            throw new EOFException();
        }
        // TODO: assert negative
        return (((long)(buffer[7] & 255) << 56) +
                ((long)(buffer[6] & 255) << 48) +
		        ((long)(buffer[5] & 255) << 40) +
                ((long)(buffer[4] & 255) << 32) +
                ((long)(buffer[3] & 255) << 24) +
                ((long)(buffer[2] & 255) << 16) +
                ((long)(buffer[1] & 255) <<  8) +
                ((long)(buffer[0] & 255) <<  0));
    }

    public static int readuint(InputStream in, byte[] buffer) throws IOException {
        int n = in.read(buffer, 0, 4);
        if (n != 4) {
            throw new EOFException();
        }
        // TODO: assert negative
        return (((buffer[3] & 255) << 24) +
                ((buffer[2] & 255) << 16) +
                ((buffer[1] & 255) <<  8) +
                ((buffer[0] & 255) <<  0));
    }

    public static int readushort(InputStream in, byte[] buffer) throws IOException {
        int n = in.read(buffer, 0, 2);
        if (n != 2) {
            throw new EOFException();
        }
        // TODO: assert negative
        return (((buffer[1] & 255) <<  8) +
                ((buffer[0] & 255) <<  0));
    }

    public static int readubyte(InputStream in) throws IOException {
        int x = in.read();
        if (x == -1) {
            throw new EOFException();
        }
        return x & 0xff;
    }

    public static void skipStr(InputStream in, String text, byte[] buffer) throws IOException {
        int n = in.read(buffer, 0, text.length());
        if (n != text.length()) {
            throw new EOFException();
        }
        for (int i = 0; i < n; i++) {
            if (buffer[i] != text.charAt(i)) {
                throw new RuntimeException("Expect: "+text);
            }
        }
    }
    public static void skipStr(RandomAccessFile in, String text, byte[] buffer) throws IOException {
        int n = in.read(buffer, 0, text.length());
        if (n != text.length()) {
            throw new EOFException();
        }
        for (int i = 0; i < n; i++) {
            if ((buffer[i]&0xff) != text.charAt(i)) {
                throw new RuntimeException("Expect: "+text);
            }
        }
    }
}
